using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    /*// QLD
    protected string siteURL = "http://www.police.qld.gov.au/News+and+Alerts/Media+Releases/";
    protected string urlRX = @"href\s*=\s*(?:\'(?<Link>.*?(/News\+and\+Alerts/Media\+Releases/20).*?[^\']*)\')";
    protected string textRX = @"<span.*?(HeadingPlaceholder2).*?><h1>(?<title>.*)</h1></span>(.|\n)*<span.*?(FCKeditorPlaceholder2).*?><P>(?<text>(.|\n)*)</P></span>";*/

    /*// NSW
    protected string siteURL = "http://www.police.nsw.gov.au/news";
    protected string urlRX = @"href\s*=\s*(?:\'(?<Link>.*?(\?sq_content_src).*?[^\']*)\')";
    protected string textRX = @"<h1>(?<title>.*)</h1><em>(?<date>.*)</em>(.|\n)*<p>(?<text>(.|\n)*)</p>";*/

    // London
    protected string siteURL = "http://cms.met.police.uk/news";
    protected string urlRX = "><a.href\\s*=\\s*(?:\\\"(?<Link>.*?(news/appeals).*?[^\\\"]*)\\\")";
    protected string textRX = "<title>(?<title>.*)</title>(.|\\n)*<div(.|\\n)*(headline)(.|\\n)*</div>(?<text>(.|\\n)*)<table(.|\\n)*<em>(?<date>.*)</em>";

    protected void ButClick(object sender, EventArgs e)
    {
        int nRec = System.Convert.ToInt32(TextBox1.Text, 10);
        WebClient client = new WebClient();
        string Msg = client.DownloadString(siteURL);

        Regex rx = new Regex(urlRX);
        MatchCollection mc = rx.Matches(Msg);

        int n = 0;
        Label1.Text = String.Format("Result({0}):<br />", (nRec == 0) ? mc.Count : nRec);

        foreach(Match links in mc)
        {
            if (n >= nRec && nRec != 0) break;
            string link = links.Groups["Link"].ToString();
            if (!Regex.IsMatch(link, "http://[a-z.]*"))
            {
                link = Regex.Match(siteURL, "http://[a-z.]*").ToString() + link;
            }
            Label1.Text += String.Format("<a href=\"{0}\">{0}</a><br /><br />{1}", link, getData(link, textRX));
            n++;
        } 
    }

    protected static string getData(string URL, string textRX)
    {
        WebClient client = new WebClient();
        string Msg = client.DownloadString(URL);
        Regex rx = new Regex(textRX);
        Match m;
        if ((m = rx.Match(Msg)).Success)
        {
            return String.Format("<b>{0}</b><br /><i>{1}</i><br />{2}<br />Lat: {3}<br />Long: {4}<hr />", m.Groups["title"], m.Groups["date"], m.Groups["text"], m.Groups["lat"], m.Groups["long"]);
        }
        else
        {
            return "nothing</br>";
        }
    } 
}
